package cn.iocoder.yudao.module.iot.controller.admin.devicedataupload;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.iot.controller.admin.devicedataupload.vo.*;
import cn.iocoder.yudao.module.iot.dal.dataobject.devicedataupload.DeviceDataUploadDO;
import cn.iocoder.yudao.module.iot.convert.devicedataupload.DeviceDataUploadConvert;
import cn.iocoder.yudao.module.iot.service.devicedataupload.DeviceDataUploadService;

@Tag(name = "管理后台 - 物联网设备上报数据记录")
@RestController
@RequestMapping("/iot/device-data-upload")
@Validated
public class DeviceDataUploadController {

    @Resource
    private DeviceDataUploadService deviceDataUploadService;

    @PostMapping("/create")
    @Operation(summary = "创建物联网设备上报数据记录")
    @PreAuthorize("@ss.hasPermission('iot:device-data-upload:create')")
    public CommonResult<Integer> createDeviceDataUpload(@Valid @RequestBody DeviceDataUploadCreateReqVO createReqVO) {
        return success(deviceDataUploadService.createDeviceDataUpload(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新物联网设备上报数据记录")
    @PreAuthorize("@ss.hasPermission('iot:device-data-upload:update')")
    public CommonResult<Boolean> updateDeviceDataUpload(@Valid @RequestBody DeviceDataUploadUpdateReqVO updateReqVO) {
        deviceDataUploadService.updateDeviceDataUpload(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除物联网设备上报数据记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('iot:device-data-upload:delete')")
    public CommonResult<Boolean> deleteDeviceDataUpload(@RequestParam("id") Integer id) {
        deviceDataUploadService.deleteDeviceDataUpload(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得物联网设备上报数据记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:device-data-upload:query')")
    public CommonResult<DeviceDataUploadRespVO> getDeviceDataUpload(@RequestParam("id") Integer id) {
        DeviceDataUploadDO deviceDataUpload = deviceDataUploadService.getDeviceDataUpload(id);
        return success(DeviceDataUploadConvert.INSTANCE.convert(deviceDataUpload));
    }

    @GetMapping("/list")
    @Operation(summary = "获得物联网设备上报数据记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('iot:device-data-upload:query')")
    public CommonResult<List<DeviceDataUploadRespVO>> getDeviceDataUploadList(@RequestParam("ids") Collection<Integer> ids) {
        List<DeviceDataUploadDO> list = deviceDataUploadService.getDeviceDataUploadList(ids);
        return success(DeviceDataUploadConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得物联网设备上报数据记录分页")
    @PreAuthorize("@ss.hasPermission('iot:device-data-upload:query')")
    public CommonResult<PageResult<DeviceDataUploadRespVO>> getDeviceDataUploadPage(@Valid DeviceDataUploadPageReqVO pageVO) {
        PageResult<DeviceDataUploadDO> pageResult = deviceDataUploadService.getDeviceDataUploadPage(pageVO);
        return success(DeviceDataUploadConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出物联网设备上报数据记录 Excel")
    @PreAuthorize("@ss.hasPermission('iot:device-data-upload:export')")
    @OperateLog(type = EXPORT)
    public void exportDeviceDataUploadExcel(@Valid DeviceDataUploadExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<DeviceDataUploadDO> list = deviceDataUploadService.getDeviceDataUploadList(exportReqVO);
        // 导出 Excel
        List<DeviceDataUploadExcelVO> datas = DeviceDataUploadConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "物联网设备上报数据记录.xls", "数据", DeviceDataUploadExcelVO.class, datas);
    }

}
